package com.example.javademo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public  class ButtonActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_button);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Button button = findViewById(R.id.button);
//        自定义内部类实现点击事件
        MyClickBtn1 mcl1 = new MyClickBtn1();
        button.setOnClickListener(mcl1);
//      自定义匿名类实现点击事件
        Button button2 = findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("TAG", "onClick:点击了btn2 " );
            }
        });

//        用本类实现点击事件
        Button button3 = findViewById(R.id.button3);
        button3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Log.e("TAG", "onClick:点击了btn3 " );
    }

    class MyClickBtn1 implements View.OnClickListener {
        @Override
        public void onClick(View v) {

            Log.e("TAG", "onClick:点击了btn1 " );
        }
    }

/*
*   被点击的控件方法
*   View: 被点击的控件
* */
public void clickBtn4(View view) {
    // Log.e("TAG", "onClick:点击了btn4 ");
//    switch 语句在 case 标签中要求使用编译时常量，而 Android 中的 R.id 资源 ID 不是编译时常量，因此不能直接用于 switch 语句
    int id = view.getId();
    if (id == R.id.button4) {
        Log.e("TAG", "点击了 button4");
    } else if (id == R.id.button5) {
        Log.e("TAG", "点击了 button5");
    }
}
}